﻿// Apache-2.0 License
// Copyright (c) 2021-2022 SuperJeff
// 微信:15906588664  QQ:1024489

using Admin.Net.InformationPlatform.Entity;
using Admin.Net.InformationPlatform.Service.SRM.Dto;
using Admin.NET.Application.Const;
using Admin.NET.Core;
using AngleSharp.Io;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Masuit.Tools;
using Microsoft.AspNetCore.Mvc;
using Nest;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.Net.InformationPlatform.Service.SRM
{
    /// <summary>
    /// SRM管理
    /// </summary>
    [ApiDescriptionSettings(groups: "SRM", Name = "模具管理", Tag = "模具管理", Order = 200)]

    public class SrmMouldService : IDynamicApiController, ITransient
    {
        /// <summary>
        /// 单例数据链接
        /// </summary>
        ISqlSugarClient db;
        /// <summary>
        /// T100数据库
        /// </summary>
        private SqlSugarProvider _T100Db;
        private SqlSugarProvider _SjDb;
        private SqlSugarProvider _Dilon;

        /// <summary>
        /// 用户信息
        /// </summary>
        TokenUser _User;


        /// <summary>
        /// srm采购管理模块
        /// </summary>
        /// <param name="db"></param>
        public SrmMouldService(ISqlSugarClient db)
        {
            this.db = db;
            this._T100Db = this.db.AsTenant().GetConnection(T100Const.ConfigId);
            this._SjDb = this.db.AsTenant().GetConnection(SJDB.ConfigId);
            this._Dilon = this.db.AsTenant().GetConnection(Dilon.ConfigId);

            // 获取 `Jwt` 存储的信息 企业编号与据点信息
            _User = Util.SjCommonUtil.GetT100UserTokenInfo(_T100Db,_Dilon);


        }
        /// <summary>
        ///  获取模具列表-分页
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SRM/GetMouldPagedList")]
        public async Task<SqlSugarPagedList<SRMMJ>> GetMouldPagedList(SrmMouldInput input)
        {
            DateTime starttime = DateTime.MinValue; DateTime endtime = DateTime.MinValue;
            if (input.StartDate != null)
                starttime = (input.StartDate.ToDateTime().ToString("yyyy-MM-dd") + " 00:00:00").ToDateTime();
            if (input.EndDate != null)
                endtime = (input.EndDate.ToDateTime().ToString("yyyy-MM-dd") + " 23:59:59").ToDateTime();

            var output = await _SjDb.Queryable<SRMMJ>()
.Where(l => l.SRMISDELETE == 0)

.WhereIF(input.EndDate != null, (l) => (l.SRMJDRQ <= endtime)) //结束日期
.WhereIF(input.StartDate != null, (l) => (l.SRMJDRQ >= input.StartDate)) //开始日期
.WhereIF(!string.IsNullOrEmpty(input.SRMMJBH), (l) => (l.SRMMJBH.Contains(input.SRMMJBH)))//模具编号
.WhereIF(!string.IsNullOrEmpty(input.SRMPM), (l) => (l.SRMPM.Contains(input.SRMPM)))//品名
.WhereIF(!string.IsNullOrEmpty(input.SRMPH), (l) => (l.SRMPH.Contains(input.SRMPH)))//品号
.WhereIF(input.SRMZT >= 0, (l) => (l.SRMZT == input.SRMZT))//状态

.OrderBy(l => l.SRMJDRQ, OrderByType.Desc) //模具建档日期排序
.Select(l => new SRMMJ
{
    SRMMJID = l.SRMMJID,
    SRMMJBH = l.SRMMJBH,
    SRMPM = l.SRMPM,
    SRMPH = l.SRMPH,
    SRMZCBH = l.SRMZCBH,
    SRMMJLX = l.SRMMJLX,
    SRMGG = l.SRMGG,
    SRMJDRQ = l.SRMJDRQ,
    SRMZT = l.SRMZT,
    SRMREMARK = l.SRMREMARK,
    SRMISDELETE = l.SRMISDELETE,
    SRMZMJSL = l.SRMZMJSL

}).ToPagedListAsync(input.Page, input.PageSize);
            return output;
        }
        /// <summary>
        /// 增加模具档案
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SRM/AddMould")]
        public async Task<string> AddMould(SRMMJ input)
        {
            //判断模具编号是否重复
            int count = await _SjDb.Queryable<SRMMJ>()
                .Where(a => a.SRMMJBH == input.SRMMJBH)
                .Where(a => a.SRMISDELETE == 0)
                .Where(a => a.SRMZT == 0 || a.SRMZT == 1)//模具正常情况下编号不能重复
                .Select(a => a.SRMMJID)
                .CountAsync();

            if (count != 0)
                throw Oops.Oh("模具编号不能重复");
            var details = new SRMMJ
            {
                SRMMJID = Guid.NewGuid().ToString("N"),
                SRMMJBH = input.SRMMJBH,
                SRMPM = input.SRMPM,
                SRMPH = input.SRMPH,
                SRMZCBH = input.SRMZCBH,
                SRMMJLX = input.SRMMJLX,
                SRMGG = input.SRMGG,
                SRMJDRQ = input.SRMJDRQ,
                SRMZT = input.SRMZT,
                SRMREMARK = input.SRMREMARK,
                SRMISDELETE = 0,
                SRMZMJSL = input.SRMZMJSL
            };

            #region 插入事务
            try
            {
                //开启事务
                _SjDb.Ado.BeginTran();
                int cnt = await _SjDb.Insertable<SRMMJ>(details).ExecuteCommandAsync();

                if (cnt == 0)
                {
                    throw Oops.Oh($"新建任务失败！");
                }
                //提交事务
                _SjDb.Ado.CommitTran();
                return "插入成功";
            }
            catch (Exception e)
            {
                //回滚事务
                _SjDb.Ado.RollbackTran();
                throw Oops.Oh($"异常：{e.Message}");
            }
            #endregion

        }
        /// <summary>
        /// 模具档案更改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/SRM/UpdateMould")]
        public async Task<string> UpdateMould(SRMMJ input)
        {
            if ((new int[] { 0 }).Contains(input.SRMZT))
            {
                //判断单据存在
                //int cnt = await _SjDb.Queryable<SRMMJ>()
                //    .Where(a => a.SRMMJID == input.SRMMJID)
                //    .Select(a => a.SRMMJID)
                //    .CountAsync();

                var cnt = await _SjDb.Queryable<SRMMJ>()
                                .Where(a => a.SRMMJID == input.SRMMJID)
                                .Select(l => new SRMMJ
                                {
                                    SRMMJID = l.SRMMJID,
                                    SRMMJBH = l.SRMMJBH,
                                    SRMZT = l.SRMZT,
                                }).FirstAsync();

                if (cnt == null)
                {
                    throw Oops.Oh("任务不存在，驳回！");
                }
                if (cnt.SRMZT == 1)//外借中不允许修改模具档案
                {
                    throw Oops.Oh("当前状态不允许修改！");
                }
                else
                {
                    //提交
                    try
                    {
                        _SjDb.Ado.BeginTran();
                        int count = await _SjDb.Updateable<SRMMJ>()
                            .SetColumns(a => a.SRMPM == input.SRMPM)
                            .SetColumns(a => a.SRMPH == input.SRMPH)
                            .SetColumns(a => a.SRMGG == input.SRMGG)
                            .SetColumns(a => a.SRMMJBH == input.SRMMJBH)
                            .SetColumns(a => a.SRMZCBH == input.SRMZCBH)
                            .SetColumns(a => a.SRMMJLX == input.SRMMJLX)
                            .SetColumns(a => a.SRMJDRQ == input.SRMJDRQ)
                            //.SetColumns(a => a.SRMZT == input.SRMZT)
                            .SetColumns(a => a.SRMREMARK == input.SRMREMARK)
                            .SetColumns(a => a.SRMZMJSL == input.SRMZMJSL)
                            .Where(a => a.SRMMJID == input.SRMMJID)

                            .ExecuteCommandAsync();

                        if (count != 1)
                        {
                            throw Oops.Oh("修改失败！");
                        }
                        _SjDb.Ado.CommitTran();
                        return "修改成功！";
                    }
                    catch (Exception e)
                    {
                        _SjDb.Ado.RollbackTran();
                        throw Oops.Oh($"异常：{e.Message}");
                    }
                }

            }
            else
            {
                throw Oops.Oh("状态码提交异常，驳回！");
            }

        }
        /// <summary>
        /// 模具档案删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/SRM/DeleteMould")]
        public async Task<string> DeleteMould(DeleteMould input)
        {
            //判断单据存在
            int cnt = await _SjDb.Queryable<SRMMJ>()
                .Where(a => a.SRMMJID == input.SRMMJID)
                .Select(a => a.SRMMJID)
                .CountAsync();
            if (cnt == 0)
            {
                throw Oops.Oh("任务不存在，驳回！");
            }
            else
            {
                //提交
                try
                {
                    _SjDb.Ado.BeginTran();
                    int count = await _SjDb.Updateable<SRMMJ>()
                        .SetColumns(a => a.SRMISDELETE == 1)
                        .Where(a => a.SRMMJID == input.SRMMJID)
                        .ExecuteCommandAsync();

                    if (count != 1)
                    {
                        throw Oops.Oh("删除失败！");
                    }
                    _SjDb.Ado.CommitTran();
                    return "删除成功！";
                }
                catch (Exception e)
                {
                    _SjDb.Ado.RollbackTran();
                    throw Oops.Oh($"异常：{e.Message}");
                }
            }
        }
        /// <summary>
        /// 模具档案状态更改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/SRM/UpdateMouldZT")]
        public async Task<string> UpdateMouldZT(UpdateMouldZT input)
        {
            if ((new int[] { 0, 1, 2 }).Contains(input.SRMZT))
            {
                //判断单据存在
                //int cnt = await _SjDb.Queryable<SRMMJ>()
                //    .Where(a => a.SRMMJID == input.SRMMJID)
                //    .Select(a => a.SRMMJID)
                //    .CountAsync();

                var cnt = await _SjDb.Queryable<SRMMJ>()
                  .Where(a => a.SRMMJID == input.SRMMJID)
                  .Select(l => new SRMMJ
                  {
                      SRMMJID = l.SRMMJID,
                      SRMMJBH = l.SRMMJBH,
                      SRMZT = l.SRMZT,
                  }).FirstAsync();

                if (cnt == null)
                {
                    throw Oops.Oh("任务不存在，驳回！");
                }
                if (cnt.SRMZT == 1 && input.SRMZT == 2)
                {
                    throw Oops.Oh("模具外借中，不能作废");
                }
                if (cnt.SRMZT == 2 && input.SRMZT == 1)
                {
                    throw Oops.Oh("模具已作废，不能外借");
                }
                else
                {
                    //提交
                    try
                    {
                        _SjDb.Ado.BeginTran();
                        int count = await _SjDb.Updateable<SRMMJ>()
                            .SetColumns(a => a.SRMZT == input.SRMZT)
                            .Where(a => a.SRMMJID == input.SRMMJID)
                            .ExecuteCommandAsync();

                        if (count != 1)
                        {
                            throw Oops.Oh("修改状态失败！");
                        }


                        _SjDb.Ado.CommitTran();
                        return "提交成功！";
                    }
                    catch (Exception e)
                    {
                        _SjDb.Ado.RollbackTran();
                        throw Oops.Oh($"异常：{e.Message}");
                    }
                }

            }
            else
            {
                throw Oops.Oh("状态码提交异常，驳回！");
            }

        }
        /// <summary>
        /// 模具下拉框列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SRM/GetMouldList")]
        public async Task<List<SRMMJ>> GetPidOrgList()
        {
            return await _SjDb.Queryable<SRMMJ>().AS("SJADMIN.SRMMJ")
                        .Where(l => l.SRMISDELETE == 0)
                        .Where(l => l.SRMZT == 0)//只有入库中的模具可以外借

                       .Select(l => new SRMMJ
                       {
                           SRMMJID = l.SRMMJID,
                           SRMMJBH = l.SRMMJBH,

                       }).ToListAsync();
        }
        /// <summary>
        /// 增加模具外借信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SRM/AddMouldCheckoutList")]
        public async Task<string> AddMouldCheckoutList(SRMMJ_LIST[] input)
        {
            List<SRMMJ_LIST> data = new List<SRMMJ_LIST>();
            string mjid = "";//需要更改的模具ID
            //初始化插入内容
            for (int i = 0; i < input.Length; i++)
            {
                if (!string.IsNullOrEmpty(input[i].SRMMJID) && !string.IsNullOrEmpty(input[i].SRMGYSID))//模具Id，供应商Id不为空时，可以添加
                {
                    var cnt = await _SjDb.Queryable<SRMMJ>()
                .Where(a => a.SRMMJID == input[i].SRMMJID)
                .Select(l => new SRMMJ
                {
                    SRMMJID = l.SRMMJID,
                    SRMMJBH = l.SRMMJBH,
                    SRMZT = l.SRMZT,
                }).FirstAsync();
                    //判断模具状态，模具归档中才能外借
                    if (cnt.SRMZT == 0)
                    {
                        SRMMJ_LIST model = new SRMMJ_LIST()
                        {
                            SRMMJLID = Guid.NewGuid().ToString("N"),
                            SRMMJID = input[i].SRMMJID,
                            SRMGYSID = input[i].SRMGYSID,
                            SRMYQGHRQ = input[i].SRMYQGHRQ,
                            SRMSJGHRQ = input[i].SRMSJGHRQ,
                            SRMCKRQ = input[i].SRMCKRQ,
                            SRMZT = input[i].SRMZT,
                            SRMREMARK = input[i].SRMREMARK,
                            SRMSS = input[i].SRMSS,
                            SRMSSZT = input[i].SRMSSZT,
                            SRMCREATE = DateTime.Now,
                            SRMCZR = _User.UserId,
                        };
                        //判断模具是否已被选择
                        int mj = data.ToList().Where(a => a.SRMMJID == input[i].SRMMJID).Select(a => a.SRMMJLID).Count();
                        if (mj == 0)
                        {
                            data.Add(model);
                            mjid += input[i].SRMMJID + ",";
                        }


                    }
                }
            }
            try
            {
                //开启事务
                _SjDb.Ado.BeginTran();
                //新增数据
                int cnt = await _SjDb.Insertable<SRMMJ_LIST>(data).ExecuteCommandAsync();
                if (cnt == 0)
                {
                    throw Oops.Oh($"新建任务失败！");
                }
                //更改模具状态
                await _SjDb.Updateable<SRMMJ>()
                    .SetColumns(a => a.SRMZT == 1)
                    .Where(a => mjid.Contains(a.SRMMJID))
                    .ExecuteCommandAsync();

                //提交事务
                _SjDb.Ado.CommitTran();
            }
            catch (Exception e)
            {
                //回滚事务
                _SjDb.Ado.RollbackTran();
                throw Oops.Oh($"异常：{e.Message}");
            }
            return "插入成功";

        }

        /// <summary>
        /// 模具外借详情状态更改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/SRM/UpdateMouldListZT")]
        public async Task<string> UpdateMouldListZT(UpdateMouldListZT input)
        {
            if ((new int[] { 0, 1, 2, 3 }).Contains(input.SRMZT))
            {
                //判断单据存在
                //int cnt = await _SjDb.Queryable<SRMMJ_LIST>()
                //    .Where(a => a.SRMMJLID == input.SRMMJLID)
                //    .Select(a => a.SRMMJLID)
                //    .CountAsync();
                var cnt = await _SjDb.Queryable<SRMMJ_LIST>()
                .Where(a => a.SRMMJLID == input.SRMMJLID)
                .Select(l => new SRMMJ_LIST
                {
                    SRMMJLID = l.SRMMJLID,
                    SRMZT = l.SRMZT,
                    SRMMJID = l.SRMMJID,
                }).FirstAsync();

                if (cnt == null)
                {
                    throw Oops.Oh("任务不存在，驳回！");
                }
                if (cnt.SRMZT == 0 && input.SRMZT != 2)//外借模具状态只能更改为2需归还状态
                {
                    throw Oops.Oh("无效操作");
                }
                if (cnt.SRMZT == 1)//已归还的外借模具状态无法更改
                {
                    throw Oops.Oh("无效操作");
                }
                if (cnt.SRMZT == 2 && input.SRMZT != 3)//需归还的外借模具状态只能等供应商确认归还
                {
                    throw Oops.Oh("无效操作");
                }
                if (cnt.SRMZT == 3 && input.SRMZT != 1)//供应商待确认归还数据只能等采购部确认归还
                {
                    throw Oops.Oh("无效操作");
                }

                else
                {
                    //提交
                    try
                    {
                        _SjDb.Ado.BeginTran();
                        int count = 0;
                        if (input.SRMZT == 1)//确认归还后更改实际归还日期
                        {
                            count = await _SjDb.Updateable<SRMMJ_LIST>()
                           .SetColumns(a => a.SRMZT == input.SRMZT)
                           .SetColumns(a => a.SRMSJGHRQ == DateTime.Now)
                           .Where(a => a.SRMMJLID == input.SRMMJLID)
                           .ExecuteCommandAsync();
                            //更改模具档案状态
                            await _SjDb.Updateable<SRMMJ>()
                  .SetColumns(a => a.SRMZT == 0)
                  .Where(a => a.SRMMJID == cnt.SRMMJID)
                  .ExecuteCommandAsync();
                        }
                        else
                        {
                            count = await _SjDb.Updateable<SRMMJ_LIST>()
                          .SetColumns(a => a.SRMZT == input.SRMZT)
                          .Where(a => a.SRMMJLID == input.SRMMJLID)
                          .ExecuteCommandAsync();
                        }



                        if (count != 1)
                        {
                            throw Oops.Oh("修改状态失败！");
                        }
                        _SjDb.Ado.CommitTran();
                        return "提交成功！";
                    }
                    catch (Exception e)
                    {
                        _SjDb.Ado.RollbackTran();
                        throw Oops.Oh($"异常：{e.Message}");
                    }
                }
            }
            else
            {
                throw Oops.Oh("状态码提交异常，驳回！");
            }

        }
        /// <summary>
        /// 模具外借详情申诉状态更改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/SRM/UpdateMouldListSSZT")]
        public async Task<string> UpdateMouldListSSZT(UpdateMouldListSSZT input)
        {
            if ((new int[] { 0, 1, 2 }).Contains(input.SRMSSZT))
            {
                //判断单据存在
                //int cnt = await _SjDb.Queryable<SRMMJ_LIST>()
                //    .Where(a => a.SRMMJLID == input.SRMMJLID)
                //    .Select(a => a.SRMMJLID)
                //    .CountAsync();
                var cnt = await _SjDb.Queryable<SRMMJ_LIST>()
                .Where(a => a.SRMMJLID == input.SRMMJLID)
                .Select(l => new SRMMJ_LIST
                {
                    SRMMJLID = l.SRMMJLID,
                    SRMSSZT = l.SRMSSZT,
                }).FirstAsync();

                if (cnt == null)
                {
                    throw Oops.Oh("任务不存在，驳回！");
                }
                if (cnt.SRMSSZT == 0 && input.SRMSSZT != 1)//无申诉状态只能变成申诉状态
                {
                    throw Oops.Oh("无效操作");
                }

                if (cnt.SRMSSZT == 1 && input.SRMSSZT != 2)//需归还的外借模具状态只能等供应商确认归还
                {
                    throw Oops.Oh("无效操作");
                }
                if (input.SRMSSZT == 0)
                {
                    throw Oops.Oh("无效操作");
                }

                else
                {
                    //提交
                    try
                    {
                        _SjDb.Ado.BeginTran();
                        int count = await _SjDb.Updateable<SRMMJ_LIST>()
                            .SetColumns(a => a.SRMSSZT == input.SRMSSZT)
                            .SetColumns(a => a.SRMSS == input.SRMSS)
                            .Where(a => a.SRMMJLID == input.SRMMJLID)
                            .ExecuteCommandAsync();

                        if (count != 1)
                        {
                            throw Oops.Oh("修改状态失败！");
                        }
                        _SjDb.Ado.CommitTran();
                        return "提交成功！";
                    }
                    catch (Exception e)
                    {
                        _SjDb.Ado.RollbackTran();
                        throw Oops.Oh($"异常：{e.Message}");
                    }
                }
            }
            else
            {
                throw Oops.Oh("状态码提交异常，驳回！");
            }

        }
        /// <summary>
        ///  获取供应商模具借出列表-分页
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SRM/GetGYSMouldPagedList")]
        public async Task<SqlSugarPagedList<SrmMouldOutInput>> GetGYSMouldPagedList(SrmMouldListInput input)
        {
            var output = await _SjDb.Queryable<SRMMJ_LIST>()
                .LeftJoin<SRMMJ>((l, b) => (b.SRMMJID == l.SRMMJID))

    .Where(l => l.SRMGYSID.Contains(_User.UserId))//供应商看到自己的数据

    .WhereIF(input.EndDate != null, (l) => (l.SRMCKRQ <= input.EndDate)) //结束日期
    .WhereIF(input.StartDate != null, (l) => (l.SRMCKRQ >= input.StartDate)) //开始日期
    .WhereIF(!string.IsNullOrEmpty(input.SRMMJBH), (l, b) => (b.SRMMJBH.Contains(input.SRMMJBH)))//模具编号
    .WhereIF(!string.IsNullOrEmpty(input.SRMGYSID), (l, b) => (l.SRMGYSID.Contains(input.SRMGYSID)))//供应商编号
    .WhereIF(!string.IsNullOrEmpty(input.SRMPM), (l, b) => (b.SRMPM.Contains(input.SRMPM)))//品名
    .WhereIF(!string.IsNullOrEmpty(input.SRMPH), (l, b) => (b.SRMPH.Contains(input.SRMPH)))//品号


    .OrderBy(l => l.SRMCKRQ, OrderByType.Desc) //出库日期排序
    .Select((l, b) => new SrmMouldOutInput
    {
        SRMMJBH = b.SRMMJBH,
        SRMPM = b.SRMPM,
        SRMPH = b.SRMPH,
        SRMZCBH = b.SRMZCBH,
        SRMMJLX = b.SRMMJLX,
        SRMGG = b.SRMGG,
        SRMJDRQ = b.SRMJDRQ,
        SRMMJLID = l.SRMMJLID,
        SRMGYSID = l.SRMGYSID,
        SRMYQGHRQ = l.SRMYQGHRQ,
        SRMSJGHRQ = l.SRMSJGHRQ,
        SRMCKRQ = l.SRMCKRQ,
        SRMSS = l.SRMSS,
        SRMSSZT = l.SRMSSZT,
        SRMZT = l.SRMZT,
        SRMREMARK = l.SRMREMARK,

    }).ToPagedListAsync(input.Page, input.PageSize);
            return output;
        }

        /// <summary>
        ///  获取模具借出列表-分页
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SRM/GetMouldListPagedList")]
        public async Task<SqlSugarPagedList<SrmMouldOutInput>> GetMouldListPagedList(SrmMouldListInput input)
        {

            var output = await _SjDb.Queryable<SRMMJ_LIST>()
                .LeftJoin<SRMMJ>((l, b) => (b.SRMMJID == l.SRMMJID))
    .Where(l => l.SRMCZR.Contains(_User.UserId))//外借人只能看自己借出的模具外借信息
    .WhereIF(input.EndDate != null, (l) => (l.SRMCKRQ <= input.EndDate)) //结束日期
    .WhereIF(input.StartDate != null, (l) => (l.SRMCKRQ >= input.StartDate)) //开始日期
    .WhereIF(!string.IsNullOrEmpty(input.SRMMJBH), (l, b) => (b.SRMMJBH.Contains(input.SRMMJBH)))//模具编号
    .WhereIF(!string.IsNullOrEmpty(input.SRMGYSID), (l, b) => (l.SRMGYSID.Contains(input.SRMGYSID)))//供应商编号
    .WhereIF(!string.IsNullOrEmpty(input.SRMPM), (l, b) => (b.SRMPM.Contains(input.SRMPM)))//品名
    .WhereIF(!string.IsNullOrEmpty(input.SRMPH), (l, b) => (b.SRMPH.Contains(input.SRMPH)))//品号
    .OrderBy(l => l.SRMCKRQ, OrderByType.Desc) //出库日期排序
    .Select((l, b) => new SrmMouldOutInput
    {
        SRMMJBH = b.SRMMJBH,
        SRMPM = b.SRMPM,
        SRMPH = b.SRMPH,
        SRMZCBH = b.SRMZCBH,
        SRMMJLX = b.SRMMJLX,
        SRMGG = b.SRMGG,
        SRMMJID = b.SRMMJID,
        SRMJDRQ = b.SRMJDRQ,
        SRMMJLID = l.SRMMJLID,
        SRMGYSID = l.SRMGYSID,
        SRMYQGHRQ = l.SRMYQGHRQ,
        SRMSJGHRQ = l.SRMSJGHRQ,
        SRMCKRQ = l.SRMCKRQ,
        SRMSS = l.SRMSS,
        SRMSSZT = l.SRMSSZT,
        SRMZT = l.SRMZT,
        SRMREMARK = l.SRMREMARK,

    }).ToPagedListAsync(input.Page, input.PageSize);
            return output;
        }



        /// <summary>
        ///  根据模具档案ID获取模具外借列表-分页
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SRM/GetMouldPagedListByID")]

        public async Task<SqlSugarPagedList<SrmMouldOutInput>> GetMouldPagedListByID(MouldbInput input)
        {
            var output = await _SjDb.Queryable<SRMMJ_LIST>()
                .LeftJoin<SRMMJ>((l, b) => (b.SRMMJID == l.SRMMJID))

    .WhereIF(!string.IsNullOrEmpty(input.SRMMJID), (l) => (l.SRMMJID == input.SRMMJID))//模具ID 
    .OrderBy(l => l.SRMCKRQ, OrderByType.Desc) //出库日期排序
    .Select((l, b) => new SrmMouldOutInput
    {
        SRMMJBH = b.SRMMJBH,
        SRMPM = b.SRMPM,
        SRMPH = b.SRMPH,
        SRMZCBH = b.SRMZCBH,
        SRMMJLX = b.SRMMJLX,
        SRMGG = b.SRMGG,
        SRMJDRQ = b.SRMJDRQ,
        SRMMJLID = l.SRMMJLID,
        SRMGYSID = l.SRMGYSID,
        SRMYQGHRQ = l.SRMYQGHRQ,
        SRMSJGHRQ = l.SRMSJGHRQ,
        SRMCKRQ = l.SRMCKRQ,
        SRMSS = l.SRMSS,
        SRMSSZT = l.SRMSSZT,
        SRMZT = l.SRMZT,
        SRMREMARK = l.SRMREMARK,

    }).ToPagedListAsync(input.Page, input.PageSize);
            return output;
        }
        /// <summary>
        /// 模具外借详情更改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/SRM/UpdateMouldList")]
        public async Task<string> UpdateMouldList(SRMMJ_LIST input)
        {
            if ((new int[] { 0, 1, 2, 3 }).Contains(input.SRMZT))
            {
                //判断单据存在
                int cnt = await _SjDb.Queryable<SRMMJ_LIST>()
                    .Where(a => a.SRMMJLID == input.SRMMJLID)
                    .Select(a => a.SRMMJLID)
                    .CountAsync();
                if (cnt == 0)
                {
                    throw Oops.Oh("任务不存在，驳回！");
                }
                else
                {
                    //提交
                    try
                    {
                        _SjDb.Ado.BeginTran();
                        int count = await _SjDb.Updateable<SRMMJ_LIST>()
                            .SetColumns(a => a.SRMSS == input.SRMSS)
                            .SetColumns(a => a.SRMYQGHRQ == input.SRMYQGHRQ)
                            //.SetColumns(a => a.SRMCKRQ == input.SRMCKRQ)
                            .SetColumns(a => a.SRMREMARK == input.SRMREMARK)
                            .Where(a => a.SRMMJLID == input.SRMMJLID)
                            .ExecuteCommandAsync();

                        if (count != 1)
                        {
                            throw Oops.Oh("修改失败！");
                        }
                        _SjDb.Ado.CommitTran();
                        return "修改成功！";
                    }
                    catch (Exception e)
                    {
                        _SjDb.Ado.RollbackTran();
                        throw Oops.Oh($"异常：{e.Message}");
                    }
                }
            }
            else
            {
                throw Oops.Oh("状态码提交异常，驳回！");
            }

        }
    }

}
